package com.zhouxiaoxuan13.consumer.feign.fallback;

import com.zhouxiaoxuan13.consumer.dto.Promotion;
import com.zhouxiaoxuan13.consumer.dto.PromotionDetailDTO;
import com.zhouxiaoxuan13.consumer.feign.PromotionServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 促销服务Feign客户端降级处理类
 * 当促销服务不可用时提供默认响应
 */
@Slf4j
@Component
public class PromotionServiceFallback implements PromotionServiceClient {

    @Override
    public ResponseEntity<List<PromotionDetailDTO>> getAllPromotions() {
        log.warn("促销服务不可用，返回降级响应 - getAllPromotions");
        List<PromotionDetailDTO> fallbackList = new ArrayList<>();
        PromotionDetailDTO fallbackPromotion = createFallbackPromotionDetail();
        fallbackList.add(fallbackPromotion);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<List<PromotionDetailDTO>> getActivePromotions() {
        log.warn("促销服务不可用，返回降级响应 - getActivePromotions");
        List<PromotionDetailDTO> fallbackList = new ArrayList<>();
        PromotionDetailDTO fallbackPromotion = createFallbackPromotionDetail();
        fallbackList.add(fallbackPromotion);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<List<Promotion>> getBasicPromotions() {
        log.warn("促销服务不可用，返回降级响应 - getBasicPromotions");
        List<Promotion> fallbackList = new ArrayList<>();
        Promotion fallbackPromotion = createFallbackPromotion();
        fallbackList.add(fallbackPromotion);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<Promotion> getPromotionById(Long id) {
        log.warn("促销服务不可用，返回降级响应 - getPromotionById: {}", id);
        Promotion fallbackPromotion = createFallbackPromotion();
        fallbackPromotion.setId(id);
        return ResponseEntity.ok(fallbackPromotion);
    }

    @Override
    public ResponseEntity<Promotion> createPromotion(Promotion promotion) {
        log.warn("促销服务不可用，无法创建促销 - createPromotion");
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
    }

    @Override
    public ResponseEntity<Promotion> updatePromotion(Long id, Promotion promotion) {
        log.warn("促销服务不可用，无法更新促销 - updatePromotion: {}", id);
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
    }

    @Override
    public ResponseEntity<Void> deletePromotion(Long id) {
        log.warn("促销服务不可用，无法删除促销 - deletePromotion: {}", id);
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
    }

    @Override
    public ResponseEntity<String> syncPromotionPrices() {
        log.warn("促销服务不可用，无法同步促销价格 - syncPromotionPrices");
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
                .body("促销服务暂时不可用，请稍后重试");
    }

    @Override
    public ResponseEntity<String> syncAllActivePromotions() {
        log.warn("促销服务不可用，无法同步活跃促销 - syncAllActivePromotions");
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
                .body("促销服务暂时不可用，请稍后重试");
    }

    /**
     * 创建降级促销信息
     */
    private Promotion createFallbackPromotion() {
        Promotion promotion = new Promotion();
        promotion.setId(-1L);
        promotion.setName("服务暂时不可用");
        promotion.setDescription("促销服务正在维护中，请稍后重试");
        promotion.setFreshProductId(-1L);
        promotion.setPromotionPrice(BigDecimal.ZERO);
        promotion.setStartDate(LocalDateTime.now());
        promotion.setEndDate(LocalDateTime.now().plusDays(1));
        promotion.setActive(false);
        promotion.setCreatedTime(LocalDateTime.now());
        promotion.setUpdatedTime(LocalDateTime.now());
        return promotion;
    }

    /**
     * 创建降级促销详情信息
     */
    private PromotionDetailDTO createFallbackPromotionDetail() {
        PromotionDetailDTO dto = new PromotionDetailDTO();
        dto.setId(-1L);
        dto.setName("服务暂时不可用");
        dto.setDescription("促销服务正在维护中，请稍后重试");
        dto.setPromotionPrice(BigDecimal.ZERO);
        dto.setStartDate(LocalDateTime.now());
        dto.setEndDate(LocalDateTime.now().plusDays(1));
        dto.setActive(false);
        dto.setCreatedTime(LocalDateTime.now());
        dto.setUpdatedTime(LocalDateTime.now());
        
        // 商品信息
        dto.setFreshProductId(-1L);
        dto.setProductName("暂无商品信息");
        dto.setProductDescription("服务不可用");
        dto.setCategory("系统");
        dto.setSpecifications("N/A");
        dto.setOriginalPrice(BigDecimal.ZERO);
        dto.setStockQuantity(0);
        
        return dto;
    }
}
